﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;

namespace Isc.Core
{
    public abstract class Repository<TEntity,TKey>  where TEntity:EntityBase<TKey>
    {
        public  IDbConnection DataSouce = ConnectionProvider._dbConnection;


        public int Insert(TEntity entity,string sql)
        { 
            return DataSouce.Execute(sql, entity);
        }
         

        public async Task<int> InsertAsync(TEntity entity, string sql)
        {
            return await DataSouce.ExecuteAsync(sql, entity);
        }

        public int Udpate(TEntity entity, string sql)
        {
            
            return DataSouce.Execute(sql, entity);
        }

        public async Task<int> UpdateAsync(TEntity entity, string sql)
        {
            return await DataSouce.ExecuteAsync(sql, entity);
        }

        public int Delete(TEntity entity, string sql)
        {
            return DataSouce.Execute(sql, entity);
        }

        public async Task<int> DeleteAsync(TEntity entity, string sql)
        {
            return await DataSouce.ExecuteAsync(sql, entity);
        }


        public TEntity GetById(TKey key,string sql)
        {
            return DataSouce.Query<TEntity>(sql, new  { @Id = key }).First();
        }

        public async Task<TEntity> GetByIdAsync(TKey key,string sql)
        {
            return (await DataSouce.QueryAsync<TEntity>(sql, new { @Id = key })).First();
        }
    }
}
